package com.xt.purchasingsystem.config;

import com.xt.purchasingsystem.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author: 老姚
 * Date:  2020/5/22 19:37
 */
@Component
@EnableConfigurationProperties(JwtProperties.class)
public class LoginGateWayFilter implements GlobalFilter, Ordered {
    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取jwt类型的token信息
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//
//        if (request.getURI().toString().indexOf("PURCHASE-AUTHORSERVER/author/accredit")!=-1){
//            return chain.filter(exchange);
//        }
//        if (request.getURI().toString().indexOf("PURCHASE-AUTHORSERVER/author/logOut")!=-1){
//            return chain.filter(exchange);
//        }
//        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
//        //没有cookie 拦截
//        if (CollectionUtils.isEmpty(cookies)) {
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        //判断jwt类型的cookie是否为空
//        HttpCookie cookie = cookies.getFirst(this.jwtProperties.getCookieName());
//        if (cookie == null) {
//            //拦截
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        //3.解析token 如果正常放行
//        try {
//            JwtUtils.getInfoFromToken(cookie.getValue(), this.jwtProperties.getPublicKey());
//        } catch (Exception e) {
//            e.printStackTrace();
//            //拦截
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//
//        if (request.getURI().toString().indexOf("PURCHASE-AUTHORSERVER/author/accredit")!=-1){
//            return chain.filter(exchange);
//        }
//        if (request.getURI().toString().indexOf("PURCHASE-AUTHORSERVER/author/logOut")!=-1){
//            return chain.filter(exchange);
//        }
//        if (request.getURI().toString().indexOf("PURCHASE-AUTHORITYMANAGESERVER/user/uploadUserPhoto")!=-1){
//            return chain.filter(exchange);
//        }
//
//        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
//        //没有cookie 拦截
//        if (CollectionUtils.isEmpty(cookies)) {
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        //判断jwt类型的cookie是否为空
//        HttpCookie cookie = cookies.getFirst(this.jwtProperties.getCookieName());
//        if (cookie == null) {
//            //拦截
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        //3.解析token 如果正常放行
//        try {
//            JwtUtils.getInfoFromToken(cookie.getValue(), this.jwtProperties.getPublicKey());
//        } catch (Exception e) {
//            e.printStackTrace();
//            //拦截
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
        //4.放行
        return chain.filter(exchange);

    }

    @Override
    public int getOrder() {
        return 0;
    }
}
